package LeetCode;

public class LongestValidParentheses
{
  public static int longestValidParentheses(String s) {
    // Start typing your Java solution below
    // DO NOT write main() function
    int left = 0, right = 0;
    int max = 0;

    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == '(') {
            left++;
        }
        else {
            right++;
        }

        if (left == right && right + left > max) {
            max = right + left;
        }
        else if (left < right) {
            left = 0;
            right = 0;
        }
        //left > right, just go on
    }

    left = 0;
    right = 0;
    for (int i = s.length() - 1; i >= 0; i--) {
        if (s.charAt(i) == '(') {
            left++;
        }
        else {
            right++;
        }

        if (left == right && right + left > max) {
            max = right + left;
        }
        else if (left > right) {
            left = 0;
            right = 0;
        }
        //left < right, just go on
    }

    return max;
  }

  /**
   * @param args
   */
  public static void main(String[] args)
  {
    // TODO Auto-generated method stub
    System.out.println(longestValidParentheses(")))((())"));
  }

}
